package com.menggudf.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.menggudf.dao.DiagnosisDao;
import com.menggudf.entity.Diagnosis;
import com.menggudf.model.DiagnosisModel;
import com.menggudf.model.ResultModel;
import com.menggudf.service.DiagnosisService;

@Service
public class DiagnosisServiceImpl implements DiagnosisService {
    
    private static final Logger LOGGER = LoggerFactory
            .getLogger(DiagnosisServiceImpl.class);

    @Autowired
    private DiagnosisDao diagnosisDao; 
    
    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public ResultModel getDiagnosisByIllnessId(String illnessId) {
        try {
            Diagnosis diagnosis = diagnosisDao.getDiagnosisByIllnessId("from Diagnosis where illnessId='"+illnessId+"'");
            DiagnosisModel dm = new DiagnosisModel();
            dm.setIllnessId(diagnosis.getIllnessId());
            dm.setScheme(diagnosis.getScheme());
            return ResultModel.success("获取成功",dm);
        } catch (Exception e) {
            LOGGER.error("getDiagnosisByIllnessId fail",e);
            return ResultModel.fail();
        }
    }

    @Override
    @Transactional(value = "medicalTM", rollbackFor=Exception.class)
    public ResultModel updateDiagnosisByIllnessId(String illnessId, String scheme) {
        try {
            Diagnosis diagnosis = diagnosisDao.getDiagnosisByIllnessId("from Diagnosis where illnessId='"+illnessId+"'");
            if(diagnosis==null){
                Diagnosis d = new Diagnosis();
                d.setIllnessId(illnessId);
                d.setScheme(scheme);
                diagnosisDao.persist(d);
                return ResultModel.success();
            }else{
                diagnosis.setScheme(scheme);
                diagnosisDao.persist(diagnosis);
                return ResultModel.success();
            }
        } catch (Exception e) {
            LOGGER.error("updateDiagnosisByIllnessId fail",e);
            return ResultModel.fail();
        }
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public String getschemeByIllnessId(String illnessId) {
        Diagnosis diagnosis = diagnosisDao.getDiagnosisByIllnessId("from Diagnosis where illnessId='"+illnessId+"'");
        String scheme = null;
        if(diagnosis!=null){
            scheme = diagnosis.getScheme();
        }
        return scheme;
    }

}
